# coding:utf-8

import paramiko
from flask import current_app


def conn_ssh(conn_info):
    current_app.logger.info(conn_info)
    current_app.logger.info("beginning to connect to the host of " + conn_info[0])

    hostIp = conn_info[0]
    port = conn_info[1]
    username = conn_info[2]
    password = conn_info[3]

    try:
        # transport = paramiko.Transport((hostIp, port))
        # transport.banner_timeout = 300;
        s = paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 跳过了远程连接中选择'是'的环节
        s.connect(hostIp, port=port, username=username, password=password, allow_agent=False, look_for_keys=False)
    except Exception as e:
        current_app.logger.error('connect error：%s', e.message)
        return e.message
    else:
        current_app.logger.info("connect successful")
        return s
